<?php
class User extends AppModel {
	var $name = 'User';
	var $displayField = 'name';
	//The Associations below have been created with all possible keys, those that are not needed can be removed
        var $virtualFields=array(
//            'fullName'=>'CONCAT(User.name," ",User.surname," - ",User.id_code," - ",User.id_erp)',
//            'strName'=>'CONCAT(UPPER(SUBSTR(User.surname,1,1)),".",UPPER(User.name))',
//            'UsName'=>'CONCAT(UPPER(LEFT(User.surname,1)),LOWER(RIGHT(User.surname,LENGTH(User.surname)-1))," ",UPPER(User.name))',
//            'display'=>'CONCAT(User.surname," ",User.name)',
        );
        function __construct($id = false, $table = null, $ds = null) {
            parent::__construct($id, $table, $ds);
            $this->virtualFields['fullName'] = sprintf('CONCAT(%s.name," ",%s.surname," - ",%s.id_code," - ",%s.id_erp)',  $this->alias, $this->alias, $this->alias, $this->alias);
            $this->virtualFields['strName'] = sprintf('CONCAT(UPPER(SUBSTR(%s.surname,1,1)),".",UPPER(%s.name))',  $this->alias, $this->alias);
            $this->virtualFields['UsName'] = sprintf('CONCAT(UPPER(LEFT(%s.surname,1)),LOWER(RIGHT(%s.surname,LENGTH(%s.surname)-1))," ",UPPER(%s.name))',  $this->alias, $this->alias, $this->alias, $this->alias);
            $this->virtualFields['display'] = sprintf('CONCAT(%s.surname," ",%s.name)', $this->alias, $this->alias);
        }
	var $belongsTo = array(
		'Backup' => array(
			'className' => 'Backup',
			'foreignKey' => 'backup_id',
		),
		'Manager' => array(
			'className' => 'Manager',
			'foreignKey' => 'manager_id',
		),
		'Hrreferent' => array(
			'className' => 'Hrreferent',
			'foreignKey' => 'hrreferent_id',
		),
		'Status' => array(
			'className' => 'Status',
			'foreignKey' => 'status_id',
		),
		'Jobfunction' => array(
			'className' => 'Jobfunction',
			'foreignKey' => 'jobfunction_id',
		),
		'Securitygroup' => array(
			'className' => 'Securitygroup',
			'foreignKey' => 'securitygroup_id',
		),
		'Company' => array(
			'className' => 'Company',
			'foreignKey' => 'company_id',
		),
		'Group' => array(
			'className' => 'Group',
			'foreignKey' => 'group_id',
		)
	);

	var $hasMany = array(
		'Dmdform' => array(
			'className' => 'Dmdform',
			'foreignKey' => 'user_id',
			'dependent' => false,
		),
		'Formstatus' => array(
			'className' => 'Formstatus',
			'foreignKey' => 'user_id',
			'dependent' => false,
		),
		'Action' => array(
			'className' => 'Action',
			'foreignKey' => 'action_by',
			'dependent' => false,
		),
		'Tool' => array(
			'className' => 'Tool',
			'foreignKey' => 'administrator',
			'dependent' => false,
		),
	);


	var $hasAndBelongsToMany = array(
		'Jobprofile' => array(
			'className' => 'Jobprofile',
			'joinTable' => 'jobprofiles_users',
			'foreignKey' => 'user_id',
			'associationForeignKey' => 'jobprofile_id',
			'unique' => true,
		),
                'Powerdelegation' => array(
			'className' => 'Powerdelegation',
			'joinTable' => 'powerdelegations_users',
			'foreignKey' => 'user_id',
			'associationForeignKey' => 'powerdelegation_id',
			'unique' => true,
		)
	);

        function  getUser($userId=null){
            if ($userId){
                $user= $this->find('list',array(
                    'conditions'=>array(
                        'id'=>$userId
                    ),
                    'fields'=>array('strName')
                ));
                return $user[$userId];
            }else{
                return false;
            }
        }
        function  getUserMail($userId=null){
            if ($userId){
                $user= $this->find('list',array(
                    'conditions'=>array(
                        'id'=>$userId
                    ),
                    'fields'=>array('mail')
                ));
                return $user[$userId];
            }else{
                return false;
            }
        }
        function  getUserPass($userId=null){
            if ($userId){
                $user= $this->find('list',array(
                    'conditions'=>array(
                        'id'=>$userId
                    ),
                    'fields'=>array('password')
                ));
                return $user[$userId];
            }else{
                return false;
            }
        }
        function  getUserMailsForGroup($groupId=null){
            if ($groupId){
                $users= $this->find('list',array(
                    'conditions'=>array(
                        'group_id'=>$groupId
                    ),
                    'fields'=>array('mail')
                ));
                return $users;
            }else{
                return false;
            }
        }
        function getUsersPerJobprofile($job){
            //$this->bindModel(array('belongsTo'=>array('JobprofilesUser')));
            return $this->JobprofilesUser->find('count',array('conditions'=>array('JobprofilesUser.jobprofile_id'=>$job)));
        }
        function getJobProfiles($userId){
            //$this->bindModel(array('belongsTo'=>array('JobprofilesUser')));
            $j=$this->JobprofilesUser->find('list',array(
                'conditions'=>array('JobprofilesUser.user_id'=>$userId),
                'fields'=>'JobprofilesUser.jobprofile_id'
            ));
            $js=$this->Jobprofile->find('list',array(
                'conditions'=>array('Jobprofile.id'=>$j)
            ));
            return $js;
        }

}
?>